//
//  STDSParticleDissipateEffect.h
//  STDSRenderSDKIOS
//
//  Created by 陈学明 on 2023/7/7.
//

#ifndef STDSParticleDissipateEffect_h
#define STDSParticleDissipateEffect_h

#include "STDSBaseEffect.hpp"
#include <random>
#include <time.h>
#include <memory>
#include <Basic/graphic/STDSParticle.h>

// 粒子消散效果
class STDSParticleDissipateEffect: public STDSBaseEffect {
public:
    STDSParticleDissipateEffect() {
        mEffectType = STDSEffectTypePictrueDissip;
    }
    
    ~STDSParticleDissipateEffect() {
//        printf("dealloc--STDSMosaicEffect\n");
    }
    
    void initGL() override {
//        STDSBaseEffect::initGL();
    }
    
    void setFloatArguments(const char *name, float *floatValue, int n) override {
//        mParam1 = *floatValue;
    }
    
    void setBitmapArguments(const char *nama, const char *data, int width, int height) override {
        if (strcmp(nama, "mask") == 0) {
            
        }
    }
    
    void setTexIdArguments(const char *name, unsigned int texId, int width, int height) override {
        
    }
    
public:
    shared_ptr<STDSShaderProgram> getCutProgram() {
        if (mCutProgram == nullptr) {
            mCutProgram = mContext->getProgram(kShaderSRCParticleCutout);
        }
        return mCutProgram;
    }
    
    shared_ptr<STDSShaderProgram> getDissipProgram() {
        if (mDissiptateProgram == nullptr) {
            mDissiptateProgram = mContext->getProgram(kShaderSRCParticleDissipate);
        }
        return mDissiptateProgram;
    }
    
    void draw() override {
        auto cutPro = getCutProgram();
        auto dissipPro = getDissipProgram();
        if (cutPro == nullptr || dissipPro == nullptr) {
            return;
        }
        if (mTexture == nullptr) {
            return;
        }
        drawTexture(mTexture->getId());
    }
    
    // 抠像
    void drawCut() {


    }
    
    
    // 发散
    void drawDissipate() {
//        glDisable(GL_DEPTH_TEST);
//        mDissiptateProgram->use();

//        glActiveTexture(GL_TEXTURE0);
//        mDissiptateProgram(GL_TEXTURE_2D, mTexSptId);
//        mProgParticleTex->setUniform(mProgParticleTex->getUniform(UNIF_TEXTURE), 0);

//        glActiveTexture(GL_TEXTURE1);
//        glBindTexture(GL_TEXTURE_2D, mTexture->getId());
//        mDissiptateProgram->setUniform(mDissiptateProgram->getUniform(UNIF_TEXTURE2), 1);
//
//        // 可能是ES3.0对应的点精灵的shader格式有所不同，比如gl_PointSize之类的
//        float new_strength = mParam.strength    ;
//        mDissiptateProgram->setUniform(mDissiptateProgram->getUniform("uStrength"), new_strength);
//        mDissiptateProgram->setUniform(mDissiptateProgram->getUniform("uSpeed"), mParam.speed);
//        mDissiptateProgram->setUniform(mDissiptateProgram->getUniform(UNIF_POINT_SIZE), mSpriteScale * mBasePointSize);
//        mDissiptateProgram->setUniform(mDissiptateProgram->getUniform("uDivision"), 1.f / mParam.spiriteDivision);
//        mDissiptateProgram->setUniform2f(mDissiptateProgram->getUniform(UNIF_SIZE), canvasSize.x, canvasSize.y);
//        mDissiptateProgram->setUniform2f(mDissiptateProgram->getUniform(UNIF_ANCHOR), mParam.anchorX, mParam.anchorY);
//        mDissiptateProgram->setUniform(mDissiptateProgram->getUniform("uFadeSpeed"), mParam.fadeSpeed);
//        if(!hasSetSpreadPoints){
//            mspreadAtbPoints.setVertices2D(mDispPartilc.getParticleCount(), mDispPartilc.getParticlePos());
//            mspreadAtbPoints.setParam4(mDispPartilc.getParamTex());
//            hasSetSpreadPoints = true;
//        }
//        mspreadAtbPoints.setPosVex(mDissiptateProgram->getAttrib(ATTR_POSITION));
//        mspreadAtbPoints.setPosTex(mDissiptateProgram->getAttrib(ATTR_TEXCOORD));
//        mspreadAtbPoints.setPosParam(mDissiptateProgram->getAttrib(ATTR_PARAM));
//        mspreadAtbPoints.draw();
//        glBindTexture(GL_TEXTURE_2D, 0);
//        ULOGE("drawTexParticle mDispPartilc.getParticleCount() %d new_strength %f", mDispPartilc.getParticleCount(), new_strength);
//
//        mProgParticleTex->unuse();
    }

private:
    shared_ptr<STDSShaderProgram> mCutProgram{nullptr}; // 抠像着色器
    shared_ptr<STDSShaderProgram> mDissiptateProgram{nullptr}; // 发散着色器
    shared_ptr<STDSParticleKit> mParticleStore{nullptr}; // 粒子仓库
};

#endif /* STDSParticleDissipateEffect_h */
